package com.cskaoyan.config.timer;

import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description 定时查询改变数据库优惠卷状态，查看是否过期
 */
@Configuration
public class QuartzConfig {
    private static final int SECONDS = 60;

    private static final int PAST_SENCONDS = 1800;

    //开启任务一，更新数据库优惠卷状态
    @Bean
    public JobDetail updateCouponStatusQuartz() {
        return JobBuilder.newJob(UpdateCouponStatusTask.class).withIdentity("task1").storeDurably().build();
    }

    @Bean
    public Trigger updateCouponStatusQuartzTrigger() {
        //10分钟执行一次,但为了演示可以设置为30s
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(SECONDS)
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(updateCouponStatusQuartz())
                .withIdentity("task1")
                .withSchedule(scheduleBuilder)
                .build();
    }

    //开启任务一，更新数据库优惠卷状态
    @Bean
    public JobDetail updateUserCouponStatusQuartz() {
        return JobBuilder.newJob(UpdateUserCouponStatusTask.class).withIdentity("task2").storeDurably().build();
    }

    @Bean
    public Trigger updateUserCouponStatusQuartzTrigger() {
        //10分钟执行一次,但为了演示可以设置为30s
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(SECONDS)
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(updateUserCouponStatusQuartz())
                .withIdentity("task2")
                .withSchedule(scheduleBuilder)
                .build();
    }

    //开启任务二，快过期的优惠卷通知用户
    @Bean
    public JobDetail couponPastInformQuartz() {
        return JobBuilder.newJob(CouponPastInformTask.class).withIdentity("task3").storeDurably().build();
    }

    @Bean
    public Trigger couponPastInformTrigger() {
        SimpleScheduleBuilder scheduleBuilder = SimpleScheduleBuilder.simpleSchedule()
                .withIntervalInSeconds(PAST_SENCONDS)
                .repeatForever();
        return TriggerBuilder.newTrigger().forJob(couponPastInformQuartz())
                .withIdentity("task3")
                .withSchedule(scheduleBuilder)
                .build();
    }

}

